package com.feel.mall.orderservice.controller;

import com.feel.mall.common.bean.Order;
import com.feel.mall.common.bean.OrderVO;
import com.feel.mall.common.bean.RespBean;
import com.feel.mall.common.exception.FeelException;
import com.feel.mall.common.service.OrderService;
import com.feel.mall.common.utils.JwtUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: 刘永钦
 * @Description:
 */

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 分页查询订单
     */
    @GetMapping
    RespBean getListByPage(Order condition, Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null || pageNum < 0  ? 1 : pageNum;
        pageSize = pageSize == null || pageSize < 0  ? 5 : pageSize;

        PageInfo<Order> pageInfo = (PageInfo<Order>) orderService.selectAll(condition, pageNum, pageSize);
        return RespBean.ok("查询成功", pageInfo);
    }
    /**
     * 分页查询订单
     */
    @GetMapping("/List/all")
    RespBean getList(Order condition, @RequestHeader("token") String token) {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        condition.setUserId(userId);
        List<Order> list = (List<Order>) orderService.selectAll(condition, null, null);
        return RespBean.ok("查询成功", list);
    }

    /**
     * 通过id查询订单信息
     */
    @GetMapping("/{id}")
    RespBean getInfoById(@PathVariable("id") String id) throws FeelException {
        Order order = orderService.selectById(id);
        return RespBean.ok("查询成功", order);
    }

    /**
     * 对于订单进行发货
     */
    @PutMapping("/{id}")
    RespBean sendGoods(@PathVariable("id") String id) throws FeelException {
        Order order = new Order();
        order.setId(id);

        orderService.sendGoods(order);

        return RespBean.ok("发货成功");
    }

    /**
     * 生成订单
     */
    @PostMapping
    public RespBean insertInfo(@RequestBody OrderVO orderVO, @RequestHeader("token") String token) throws FeelException {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        orderVO.setUserId(userId);

        orderService.addInfo(orderVO);
        return RespBean.ok("生成成功",orderVO);
    }

    /**
     * 余额支付
     */
    @PostMapping("/pay")
    public RespBean pay(String orderId, String payPassword, @RequestHeader("token") String token) throws FeelException {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        // 采用默认支付方式
        orderService.pay(orderId,payPassword,userId);

        return RespBean.ok("支付成功");
    }

    /**
     * 订单统计信息
     */
    @GetMapping("/Echarts")
    public RespBean getInfoForEcharts() {
        List<Map<String, Integer>> info = orderService.getInfoForEcharts();
        return RespBean.ok("查询成功",info);
    }

    /**
     * 修改用户信息
     */
    @PutMapping("/update")
    public RespBean updateInfo(@RequestBody Order order,@RequestHeader("token") String token) throws FeelException {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");
        String roles = (String) map.get("roles");

        if (roles.equals("admin")) {
            orderService.updateInfo(order,null);
        } else {
            orderService.updateInfo(order,userId);
        }
        return RespBean.ok("修改成功");
    }
}
